from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import *
import json



#链接
# 增
def links(request):
    '''
    @author:郝春虎
    @date:2018/10/23
    提交方式：POST
    提交数据方式：{'lin':'xxx'}
    '''

    if request.method == 'POST':
        info = dict(request.POST)
        if info['lin_1'][0] == '':
            return HttpResponse(json.dumps({'message':'链接不能为空'}))
        print(info)
        try:
            ls = Link.objects.get(lins=info['lin_1'][0])
        except Link.DoesNotExist:
            Link.objects.create(lins=info['lin_1'][0])
            return HttpResponse(json.dumps({'message':'添加成功'}))
        else:
            return HttpResponse(json.dumps({'message':'此链接已存在'}))
    elif request.method == 'GET':
        return render(request,'link_exp/add_link.html')

# 删
def del_link(request):
    '''
        @author:郝春虎
        @date:2018/10/23
        提交方式：POST
        提交数据方式：{'lin':'xxx'}
        '''
    if request.method == 'GET':
        return HttpResponse('访问模式为POST')
    info = dict(request.POST)
    try:
        info_1 = Link.objects.get(lins = info['lin'][0])
    except KeyError:
        return HttpResponse('格式不对')
    except Link.DoesNotExist:
        return HttpResponse('输入错误')
    else:
        info_1.delete()
        return HttpResponse('删除成功')

# 改
def fil_link(request):
    '''
        @author:郝春虎
        @date:2018/10/23
        提交方式：POST
        提交数据方式：{'lin':'xxx'}
        '''
    if request.method == 'GET':
        return HttpResponse('访问模式为POST')
    info = dict(request.POST)
    try:
        Link.objects.get(lins = info['lin'][0])
    except Link.DoesNotExist:
        return HttpResponse('没有此物')
    else:
        Link.objects.filter(lins = info['lin'][0]).upadte(lins = info['lin_1'][0])
        return HttpResponse('修改成功')



#查
def cr_link(request):
    '''
        @author:郝春虎
        @date:2018/10/23
        提交方式：POST
        提交数据方式：{'lin':'xxx'}
        '''
    if request.method == 'GET':
        return HttpResponse('访问模式为POST')
    info = dict(request.POST)
    try:
        for_lin = Link.objects.get(lins = info['lin'][0])
    except Link.DoesNotExist:
        return HttpResponse('没有找到')

    else:
        return HttpResponse('已找到({})'.format(info['lin'][0]))




# 展示链接
def link_show(request):
    """

            @author:郝春虎
        @date:2018/10/23
        提交方式：POST
        提交数据方式：{'lin':'xxx'}
    """
    link_all = Exp.objects.all()
    print(link_all)
    context = {'mess':link_all}
    return render(request,'link_exp/zong.html',context)


# 添加数据库链接

def add_all(request):
    """

    @account:郝春虎
    @time:2018/10/24
    提交方式：post
    数据库格式：{
    }
    """
    if request.method == 'GET':
        return render(request,'link_exp/add_all.html')
    info = dict(request.POST)
    try:
        lo = Link.objects.get(lins=info['lin'][0])
        #.objects.get(lins=lo)
    except Link.DoesNotExist:
        return HttpResponse('没有链接')
    else:
        Exp.objects.create(
            lins_exp=info['exp'][0],
            method = info['method'][0],
            push_args = info['args'][0],
            return_info = info['info'][0],
            lins=lo,

        )
        return HttpResponse('添加成功')
























